<!-- 用户登录界面 -->
<template>
  <div id="login">
    <div class="bg"></div>
    <el-row class="main-container">
      <el-col :lg="8" :xs="16" :md="10" :span="10">
        <div class="top">
          <i class="iconfont icon-kaoshi"></i><span class="title">调查问卷系统</span>
        </div>
        <div class="bottom">
          <span id="back" @click="goBack" class="el-icon-arrow-left" ></span>
          <div class="container">
            <p class="title">账号登录</p>
            <el-form :label-position="labelPosition" label-width="80px" :model="formLabelAlign">
              <el-form-item label="手机号">
                <el-input v-model.number="formLabelAlign.phone" placeholder="请输入手机号"></el-input>
              </el-form-item>
              <el-form-item label="验证码" prop="code">
                <span class="get-code"><a @click="sendCode">获取验证码</a></span>
                <el-input  v-model="code" placeholder="请输入验证码" ></el-input>
              </el-form-item>
              <div class="submit">
                <el-button type="primary" class="row-login" @click="login()">登录</el-button>
              </div>
              <div class="options">
                <p class="find"><a href="javascript:;" @click="reset()">找回密码</a></p>
                <div class="register">
                  <span>没有账号?</span>
                  <span><a href="javascript:;" @click="register()">去注册</a></span>
                </div>
              </div>
            </el-form>
          </div>
        </div>
      </el-col>
    </el-row>
    <el-row class="footer">
      <el-col>
        <p class="msg2">版权所有 ©2022 <!--智鹏有限公司--> 保留所有权利  <a href="http://beian.miit.gov.cn/" target="_blank">刘ICP备20204123号</a></p>
      </el-col>
    </el-row>
    <section class="remind">
      <span>管理员账号：admin</span>
      <span>租户账号：neu</span>
      <span>密码都是：123456</span>
    </section>
  </div>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: "login",
  data() {
    return {
      code: null,
      sendcode: null,
      role: 0,
      labelPosition: 'left',
      formLabelAlign: {
        phone: '13685015577',
      }
    }
  },
  methods: {
    goBack(){
            if (window.history.length <= 1) {
                this.$router.push({path:'/'})
                return false
            } else {
                this.$router.go(-1)
            }
    },
    sendCode(){
      if(this.formLabelAlign.phone == '' || this.formLabelAlign.phone == null){
        this.$message({
                showClose: true,
                type: 'error',
                message: '请输入电话'
              })
        return false
      }
      this.$axios({
            url: `/api/phone`,
            method: 'post',
            data: {
              ...this.formLabelAlign
            }
          }).then(res=>{
            let resData = res.data.message
            this.sendcode = res.data.message
            if(resData == null) { //错误提示
              this.$message({
                showClose: true,
                type: 'error',
                message: '生成验证码错误'
              })
            }else{
              this.$message({
                showClose: true,
                type: 'success',
                message: '验证码已生成'
              })
            }
          })
    },
    //用户登录请求后台处理
    login() {
      console.log("登录操作执行-------");
      this.$axios({
        url: `/api/login`,
        method: 'post',
        data: {
          ...this.formLabelAlign
        }
      }).then(res=>{
        let resData = res.data.data
        if(resData != null) {

          console.log(resData)
          switch(resData.status) {
            case "0":  //管理员
              this.$cookies.set("cname", resData.username)
              this.$cookies.set("cid", resData.id)
              this.$cookies.set("role", 0)
              this.$router.push({path: '/index' }) //跳转到首页
              break
            case "1": //租户
              this.$cookies.set("cname", resData.teacherName)
              this.$cookies.set("cid", resData.teacherId)
              this.$cookies.set("role", 1)
              this.$router.push({path: '/index' }) //跳转到租户
              break
            case "2": //用户
              this.$cookies.set("cname", resData.studentName)
              this.$cookies.set("cid", resData.studentId)
              this.$router.push({path: '/index'})
              break
            case "3": //答者
              this.$cookies.set("cname", resData.studentName)
              this.$cookies.set("cid", resData.studentId)
              this.$router.push({path: '/student'})
              break  
          }
        }
        if(resData == null) { //错误提示
          this.$message({
            showClose: true,
            type: 'error',
            message: '用户名或者密码错误'
          })
        }
      })
    },
    register(){
      this.$router.push({path:'/register'})
    },
    reset(){
      this.$router.push({path:'/reset'})
    },
    clickTag(key) {
      this.role = key
    }
  },
  computed: mapState(["userInfo"]),
  mounted() {

  }
}
</script>

<style lang="less" scoped>

#back {
  position: absolute;
  font-size: 35px;
  left: 520px;
  top: 200px;
  
}
.remind {
  border-radius: 4px;
  padding: 10px 20px;
  display: flex;
  position: fixed;
  right: 20px;
  bottom: 50%;
  flex-direction: column;
  color: #606266;
  background-color: #fff;
  border-left: 4px solid #409eff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19)
}
.container {
  margin-bottom: 32px;
}
.container .el-radio-group {
  margin: 30px 0px;
}
a:link {
  color:#ff962a;
  text-decoration:none;
}
#login {
  font-size: 14px;
  color: #000;
  background-color: #fff;
}
#login .bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow-y: auto;
  height: 100%;
  background: url('../../assets/img/loginbg.png')center top / cover no-repeat;
  background-color: #b6bccdd1 !important;
}
#login .main-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
#login .main-container .top {
  margin-top: 100px;
  font-size: 30px;
  color: #ff962a;
  display: flex;
  justify-content: center;
}
#login .top .icon-kaoshi {
  font-size: 80px;
}
#login .top .title {
  margin-top: 20px;
}
#login .bottom {
  display:flex;
  justify-content: center;
  background-color:#fff;
  border-radius: 5px;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
#login .bottom .title {
  text-align: center;
  font-size: 30px;
}
.bottom .container .title {
  margin: 30px 0px;;
}
.bottom .submit .row-login {
  width: 100%;
  background-color: #04468b;
  border-color: #04468b;
  margin: 20px 0px 10px 0px;
  padding: 15px 20px;
}
.bottom .submit {
  display: flex;
  justify-content: center;
}
.footer {
  margin-top: 50px;
  text-align: center;
}
.footer .msg1 {
  font-size: 18px;
  color: #fff;
  margin-bottom: 15px;
}
.footer .msg2 {
  font-size: 14px;
  color: #e3e3e3;
  margin-top: 70px;
}
.bottom .options {
  margin-bottom: 40px;
  color: #ff962a;
  display: flex;
  justify-content: space-between;
}
.bottom .options > a {
  color: #ff962a;
}
.bottom .options .register span:nth-child(1) {
  color: #8C8C8C;
}


.get-email{
  position: absolute;
    width: 150px;
    height: 10px;
    line-height: 20px;
    font-size: 14px;
    cursor: pointer;
    z-index: 10;
    color: #067bef;
    left: 680px;
}
.get-code {
    position: absolute;
    width: 63px;
    height: 10px;
    line-height: 20px;
    top: 10px;
    right: 5px;
    font-size: 12px;
    cursor: pointer;
    z-index: 10;
    color: #067bef;
}
</style>
